<?php
    class dataBase
	{
            public $pdo;
            protected $lastId = null;
            
                public function  __construct()
                {
                   // $pdo = connect::singleton();
//                    $sql = $pdo -> prepare("SET NAMES utf8");
//                    $sql -> execute();
                    $this->pdo = connect::singleton();
                }
		public function ExecuteQuery($query,$values,$check=DB_CHECK)
		{
			if($check)
			{
					try
					{
						$pdo = connect::singleton();
						$sql = $pdo -> prepare($query);
						$sql -> execute($values);
					
						return $sql -> fetchALL();
						}
					catch(PDOException $e)
				 	{
				 		$ex = array('ex', $e);
						echo"<pre>";
						print_r($ex);exit;
				 	}
			}
			else
			{
				try
				{
					$pdo = connect::singleton();
					$sql = $pdo -> prepare($query);
					$sql -> execute($values);
				
					return $sql -> fetchALL();
					}
				catch(PDOException $e)
			 	{
			 		$ex = 'ex';
					return $ex;
			 	}
			}
		}
		public function ModifyQuery($query,$values,$check=DB_CHECK)
		{
			if($check)
			{
					try
					{
						$pdo = connect::singleton();
						$sql = $pdo -> prepare($query);
						$sql -> execute($values);
						}
					catch(PDOException $e)
				 	{
				 		$ex = array('ex', $e);
						echo"<pre>";
						print_r($ex);exit;
				 	}
			}
			else
			{
				try
				{
					$pdo = connect::singleton();
					$sql = $pdo -> prepare($query);
					$sql -> execute($values);
					}
				catch(PDOException $e)
			 	{
			 		$ex = 'ex';
					return $ex;
			 	}
			}
		}
		
		public function PdoQuery($query,$values,$check=DB_CHECK)
		{
			if($check)
			{
					try
					{
						$pdo = connect::singleton();
						$sql = $pdo -> prepare($query);
						$sql -> execute($values);
					
						return $sql -> fetchALL(PDO::FETCH_ASSOC);
						}
					catch(PDOException $e)
				 	{
				 		$ex = array('ex', $e);
						echo"<pre>";
						print_r($ex);exit;
				 	}
			}
			else
			{
				try
				{
					$pdo = connect::singleton();
					$sql = $pdo -> prepare($query);
					$sql -> execute($values);
				
					return $sql -> fetchALL(PDO::FETCH_ASSOC);
					}
				catch(PDOException $e)
			 	{
			 		$ex = 'ex';
					return $ex;
			 	}
			}
		}
		
		private function CheckCountOfElements($sql,$values)
		{
			$tab = explode('?', $sql);
			if(count($values)==(count($tab)-1))
			{
				
			}
			print_r($tab);exit;
		}
		private function CheckQuery($query_arr,$pdo_arr){
			
			$check = true;
			
			foreach($query_arr as $key=>$value){
				if(count($pdo_arr[$key])!=substr_count($value,'?'))
					$check = false;
			}
			
			if(!$check)			
				return false;						
			else
				return true;		
		}
		
		 public function BuildQuery($pdo_query,$pdo_array){
			
			$query_arr = explode('?',$pdo_query);
			
			$result_query = '';
			
			foreach($query_arr as $key=>$value){
				
				@$result_query.=$value."'".$pdo_array[$key]."'";
					
			}
			
			return $result_query;
			
		} // end BuildQuery
                
        /**
         * Insert row to specific table
         *
         * @param string $table - table's name
         * @param array $fieldsValues array of pairs 'column_name' => 'value'
         * @param <string $seqName - - name of sequence to return last insert id
         * @author Grzegorz Wiater <g.wiater@gmail.com>
         */
        public function insert($table, $fieldsValues, $seqName = null){
            try{
                    $fields = array_keys($fieldsValues);
                    $values = array_values($fieldsValues);

                    $sql  = "INSERT INTO ".$table." (";
                    $sql .= join(", ",$fields);
                    $sql .= ") VALUES (:";
                    $sql .= join(", :",$fields);
                    $sql .= ")";
                    
                    $stmt = $this->pdo->prepare($sql);
                    foreach($fieldsValues as $key=>$val){
                            if(is_numeric($val)){
                                    $stmt->bindValue(':'.$key,$val, PDO::PARAM_INT);
                            }else{
                                    $stmt->bindValue(':'.$key,$val, PDO::PARAM_STR);
                            }
                    }

                    $stmt -> execute();
                    if(!is_null($seqName)){
                        return $this->lastId = $this->pdo->lastInsertId($seqName);
                    }
            }catch(PDOException $e){
                    echo "ERROR: ".$e->getMessage();
            }
        }

        public function getLastInsertId(){
            return $this->lastId;
	}
}
?>
